package bean;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named
@SessionScoped
public class LocaleBean implements Serializable {

	private Locale locale = Locale.ENGLISH;
	private String localeString = "";
	private ResourceBundle bundle;
	private Map<String, Object> countries;
	
	public LocaleBean() {
		bundle = ResourceBundle.getBundle("resources.messages.Messages", locale);
		countries = new LinkedHashMap<String, Object>();
		countries.put(bundle.getString("english"), Locale.ENGLISH);
		countries.put(bundle.getString("russian"), new Locale("ru"));
	}

	public void localeChange() {				
		String newLocaleValue = localeString;
		for (Map.Entry<String, Object> entry : countries.entrySet()) {
			if (entry.getValue().toString().equals(newLocaleValue)) {
				locale = (Locale) entry.getValue();
				FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
			}
		}
	}
	
	public Map<String, Object> getCountries() {
		return countries;
	}

	public Locale getLocale() {
		return locale;
	}
	
	public ResourceBundle getBundle() {
		return bundle;
	}

	public void setBundle(ResourceBundle bundle) {
		this.bundle = bundle;
	}

	public void setLocale(String locale) {		
		this.locale = new Locale(locale);
	}

	public String getLocaleString() {
		return localeString;
	}

	public void setLocaleString(String localeString) {
		this.localeString = localeString;
	}
	
}